package com.vt.service;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.vt.domain.CourseDescription;
import com.vt.domain.CourseType;
import com.vt.domain.RoomSetup;

@Path("/catalog")
public class Catalog {

	static HashMap<String, CourseDescription> catalog;

	static {
		catalog = new HashMap<String, CourseDescription>();
		List<String> objects = new ArrayList<String>();
		objects.add("premier");
		objects.add("deuxieme");
		ArrayList<String> softwares = new ArrayList<String>();
		ArrayList<String> hardwares = new ArrayList<String>();
		softwares.add("eclipse");
		softwares.add("metro");
		softwares.add("java");
		hardwares.add("pc");

		RoomSetup setup = new RoomSetup(softwares, hardwares);
		CourseDescription course1 = new CourseDescription("1234",
				"Développer des web services", 100, objects, CourseType.java,
				setup, "fr-fr", "ver2.0", "2003");
		CourseDescription course2 = new CourseDescription("3456", "java abcd",
				100, objects, CourseType.java, setup, "fr-fr", "ver3.0", "2005");
		catalog.put(course1.getCode(), course1);
		catalog.put(course2.getCode(), course2);
	}

	@Path("/year/{cid}")
	@GET
	@Produces("application/xml,application/json")
	public String getYear(@PathParam("cid") String cid) {
		return catalog.get(cid).getYear();
	}

	public String getTitle(String cid) {
		return catalog.get(cid).getTitre();
	}

	@Path("/course/{cid}")
	@GET
	@Produces("application/xml,application/json")
	public CourseDescription getCDD(@PathParam("cid") String cid) {
		return catalog.get(cid);
	}

	@Path("/course/all")
	@GET
	@Produces("application/xml,application/json")
	public List<CourseDescription> getAll() {
		List<CourseDescription> listCourse = new ArrayList<CourseDescription>();
		for (Entry<String, CourseDescription> entry : catalog.entrySet()) {
			CourseDescription valeur = entry.getValue();
			listCourse.add(valeur);
		}
		return listCourse;
	}

	@Path("/course/{cid}")
	@DELETE
	public void deleteCDD(@PathParam("cid") String cid) {
		catalog.remove(cid);
	}

	@Path("/course/{cid}")
	@POST
	@Consumes("application/json")
	public Response changeDesc(@PathParam("cid") String cid,
			CourseDescription course) {
		if (catalog.containsKey(cid)) {
			catalog.put(cid, course);
		}

		return Response.ok().build();
	}

	public RoomSetup getRoomSetup(String cid) {
		return catalog.get(cid).getSetup();
	}

	public byte[] getCDDPdf(String cid) throws InterruptedException {
		Document document = new Document(PageSize.LETTER);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			Thread.sleep(1500);
			PdfWriter.getInstance(document, baos);
			document.open();
			document.add(new Paragraph("Hello World"));
			document.add(new Paragraph(catalog.get(cid).getTitre()));
			document.close();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return baos.toByteArray();
	}
}
